package u1.Client;


import novelRead.common.*;
import java.io.*;
import java.net.Socket;
import java.util.HashMap;

public class NovelReadClient {
    private static ObjectInputStream ois = null;
    private static ObjectOutputStream oos = null;
    private static OutputStream output = null;
    private static InputStream input = null;
    private static Socket clientSocket;


    // 套接字创建类

    public static Socket startNovel(String ip, int port) {
        try {
            clientSocket = new Socket(ip, port);
            return clientSocket;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    //发送

    public static boolean send(Request requset) {
        ObjectOutputStream oos = null;
        OutputStream output = null;
        InputStream input = null;
        try {
            output = clientSocket.getOutputStream();
            input = clientSocket.getInputStream();
            oos = new ObjectOutputStream(output);
            oos.writeObject(requset);
            oos.flush();
            //oos.reset();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static Response receive() {
        ObjectInputStream ois = null;
        OutputStream output = null;
        InputStream input = null;
        try {
            input = clientSocket.getInputStream();
            output = clientSocket.getOutputStream();
            ois = new ObjectInputStream(input);
            Response response = (Response) ois.readObject();
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //注册账号

    public static Response register(User user) {
        Request requset = new Request();
        requset.put("register", user);
        send(requset);
        return receive();
    }

    // 验证登录

    public static Response login(User user) {
        Request requset = new Request();
        requset.put("longin", user);
        send(requset);
        return receive();
    }

    // 上传小说

    public static Response upload(Book book) {
        Request requset = new Request();
        requset.put("upload", book);
        send(requset);
        return receive();
    }

    // 下载书籍，返回包括书籍的response类

    public static Response download(Book book) {
        Request requset = new Request();
        requset.put("download", book);
        send(requset);
        return receive();
    }

    // 在线阅读

    public static Response readOnline(Book book,int page) {
        Request requset = new Request();
        requset.put("readOnline", book);
        requset.setDataName(String.valueOf(page));
        send(requset);
        return receive();
    }

    // 返回书的列表

    public static Response minType(String type) {
        Request requset = new Request();
        requset.put("minType", type);
        send(requset);
        return receive();

    }

    // 向服务器请求总列表

    public static Response maxType() {
        Request requset = new Request();
        HashMap<String, Integer> sizeMap = new HashMap<>();
        requset.put("maxType", sizeMap);
        send(requset);
        return receive();
    }


    public static boolean saveBookLocal(Book book){
        File file =new File(book.getPath());
        BufferedWriter bf = null;
        try {
            bf = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
            bf.write(book.getContent());
            bf.flush();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}


